package com.pateo.bjev.contacts.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.pateo.bjev.contacts.R;

/**
 * Created by pateo on 17-5-11.
 */

public class KeyGridAdapter extends RecyclerView.Adapter<KeyGridAdapter.ViewHolder> implements View.OnClickListener{

    public String[] keys = {"1","2","3","4","5","6","7","8","9","*","0","#"};

    private KeyInputCallback callback;

    int itemHeight;
    Context mContext;
    public KeyGridAdapter(Context context, RecyclerView recyclerView){
        this.mContext = context;
        itemHeight = recyclerView.getMeasuredHeight()/4;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.key_item_layout,parent,false);
        ViewHolder vh = new ViewHolder(view);
        return vh;
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        holder.key.setText(keys[position]);
    }

    @Override
    public int getItemCount() {
        return keys.length;
    }

    @Override
    public void onClick(View view) {
        if(callback!=null && view instanceof TextView){
            callback.onKeyInput(((TextView) view).getText().toString());
        }
    }

    class ViewHolder extends RecyclerView.ViewHolder{

        TextView key;

        public ViewHolder(View itemView) {
            super(itemView);
            key = (TextView) itemView;

            key.setOnClickListener(KeyGridAdapter.this);
        }
    }

    public void setKeyInputCallback(KeyInputCallback callback){
        this.callback = callback;
    }

    /**
     * 拨号盘 按键点击事件回调接口
     */
    public interface KeyInputCallback{
        void onKeyInput(String key);
    }
}
